// pages/recharge/recharge.js
var that,
  api = new(require('../../utils/api.js')),
  headimg = api.getimgurl(),
  staticimg = api.getstaticimgurl(),
  QQMapWX = require('../../utils/qqmap-wx-jssdk.js'),
qqmapsdk,
  util = new(require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hideModal:!0,
    price:'',
    storage:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    api.getuserstorage(res=>{
      if(res.data.issuccess==1){
        let storage=(res.data.rest/100).toFixed(2);
        if(storage<0) storage = 0
        that.setData({storage})
      }
    })
    // let user=wx.getStorageSync('user')
    // that.setData({storage:(user.storage/100).toFixed(2)})
  },
  input(e){
    that.setData({
      price:e.detail.value
    })
  },
  hidemodal(){
    that.setData({hideModal:!1})
  },
  onPressCancel(){
    that.setData({hideModal:!0,price:"",})
  },
  onPressComfirm(){
    let {price}=that.data
    if(price==''||isNaN(Number(price))){
      wx.showModal({
        title: '提示',
        showCancel:!1,
        content:"请输入金额"
      })
      return;
    }
    let payprice = price *100
    api.prepay({price:payprice},res=>{
      if(res.succ==1){
        wx.showLoading({
          title: '充值中...',
        })
        setTimeout(() => {
            wx.hideLoading();
            wx.showModal({
                    content:'充值成功',
                    showCancel:!1,
                  })
            that.setData({hideModal:!0},()=>{
              api.getuserstorage(res=>{
                if(res.data.issuccess==1){
                  let storage=(res.data.rest/100).toFixed(2);
                  if(storage<0) storage = 0
                  that.setData({storage})
                }
              })
            })
        }, 2000);

        // price=Math.floor(price*99.2)
        // api.afterpay({price,ordernum:res.orderid},res=>{
        //   if(res.data.issuccess==1){
        //     wx.showModal({
        //       content:'充值成功',
        //       showCancel:!1,
        //     })
        //     that.setData({hideModal:!0})
        //   }
        // })
      }
    })
  },
  pay(e){
    console.log(e,'eeee');
    let {price}=e.currentTarget.dataset
   let payprice = price *100
  
    api.prepay({price:payprice},res=>{
      if(res.succ==1){
        wx.showLoading({
          title: '充值中...',
        })
        setTimeout(() => {
            wx.hideLoading();
            wx.showModal({
                    content:'充值成功',
                    showCancel:!1,
                  })
            that.setData({hideModal:!0},()=>{
              api.getuserstorage(res=>{
                if(res.data.issuccess==1){
                  let storage=(res.data.rest/100).toFixed(2);
                  if(storage<0) storage = 0
                  that.setData({storage})
                }
              })
            })
        }, 2000);

        // price=Math.floor(price*99.2)
        // api.afterpay({price,ordernum:res.orderid},res=>{
        //   if(res.data.issuccess==1){
        //     wx.showModal({
        //       content:'充值成功',
        //       showCancel:!1,
        //     })
        //     that.setData({hideModal:!0})
        //   }
        // })
      }
    })
  },
  torecord(){
    wx.navigateTo({
      url: '../record/record',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})